<?php

class ShowAction extends CAction
{
    public function run()
    {
        if(!isset($_GET['id']))
            throw new CHttpException(400, 'The request is invalid');

        $recipe=Recipe::loadObjectById($_GET['id']);
        if($recipe===null)
            throw new CHttpException(404,'The requested recipe does not exist.');
        if ($recipe->userId==Yii::app()->user->id)
            $edit=true;
        else {
            if(!$recipe->isPublished()){
                throw new CHttpException(404,'The requested recipe is not published');
            }
            $edit=false;
        }
        
        $unsortedSteps=Step::loadObjectsByRecipeId($recipe->id);
        $steps=array();
        foreach($recipe->steps as $stepId){
            foreach($unsortedSteps as $k=>$step){
                if($step->id==$stepId){
                    $steps[] = $step;
                    unset($unsortedSteps[$k]);
                    break;
                }
            }
        }
        
        $this->controller->render('show',array(
            'recipe'=>$recipe,
            'steps'=>$steps,
            'edit'=>$edit,
        ));
    }
}
  
?>